/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.overall;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.composite.CompositeGenerator;
import com.inspur.edp.caf.generator.composite.CompositeGeneratorContext;
import com.inspur.edp.cef.generator.apiGenerator.CefApiGenerator;
import com.inspur.edp.cef.generator.configGenerator.CefConfigGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.CefCoreGenerator;
import com.inspur.edp.cef.generator.entityGenerator.BaseEntityGenerator;
import com.inspur.edp.cef.generator.repository.RepositoryGenerator;
import com.inspur.edp.cef.generator.repository.base.CefRepositoryGenerstor;
import com.inspur.edp.jittojava.context.JitContext;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.caf.generator.module.ModuleGenerator;

public abstract class CefGenerator extends CompositeGenerator {
  protected String path;
  protected GspMetadata metadata;
  protected boolean preGenerating;
  protected ModuleGenerator refGenerator;
//  protected Assembly apiAssembly;
  private boolean useRefGenCtx = false;
  private CefGeneratorContext refGenCtx;

  protected CefGenerator(
      String path,
      GspMetadata metadata,
      boolean preGenerating,
      ModuleGenerator refGenerator,
      JitContext jitContext) {
    this.path = path;
    this.metadata = metadata;
    this.preGenerating = preGenerating;
    this.refGenerator = refGenerator;
    this.jitContext = jitContext;
  }

  protected CefGenerator(
      String path,
      GspMetadata metadata,
      CefGeneratorContext refGenCtx,
      ModuleGenerator refGenerator,
      JitContext jitContext) {
    this(path, metadata, false, jitContext.getCoreModuleGenerator(), jitContext);
    useRefGenCtx = true;
    //DataValidator.CheckForNullReference(refGenCtx, "refGenCtx");
    this.refGenCtx = refGenCtx;
//    refGenCtx.setProjectApiPackageName(jitContext.getApiProjectAssembly());

    refGenCtx.initApiTypeInfos();
  }


  // keyword:
  // ORIGINAL LINE: public new CefGeneratorContext getContext() { return
  // (CefGeneratorContext)super.getContext(); }
  @Override
  public CefGeneratorContext getContext() {
    return (CefGeneratorContext) super.getContext();
  }

//  public final void setProjectApiPackageName(Assembly apiAssembly) {
//    this.apiAssembly = apiAssembly;
//  }

  @Override
  protected final CompositeGeneratorContext createCompositeContext() {
    CefGeneratorContext cefGenCtx = null;
    if (useRefGenCtx) {
      cefGenCtx = refGenCtx;
    } else {
      cefGenCtx = createCefGeneratorContext();
    }
    cefGenCtx.setMetadataJitContext(this.getMetadataJITContext());
    return cefGenCtx;
  }

  protected abstract CefGeneratorContext createCefGeneratorContext();

  @Override
  protected void beforeInitialize() {
    super.beforeInitialize();
    getContext().setMetadataPath(path);
    getContext().setPreGenerating(preGenerating);
    getContext().setMetadata(metadata);
  }

  public final void disposeFinally() {}

  protected java.util.ArrayList<String> getExtendRefPaths() {
    return new java.util.ArrayList<String>();
  }

  protected boolean getHasRepository() {
    return true;
  }

  private JitContext jitContext;

  public final JitContext getMetadataJITContext() {
    return jitContext;
  }

  @Override
  protected final java.util.ArrayList<BaseGenerator> createCompositeItemGenerators() {
    java.util.ArrayList<BaseGenerator> list = new java.util.ArrayList<BaseGenerator>();
    if (preGenerating) {
      BaseEntityGenerator entityGen = createEntityGenerator();
      list.add(entityGen);
      CefApiGenerator apiGen = createApiGenerator();
      list.add(apiGen);
    } else {
      CefCoreGenerator coreGen = createCoreGenerator();
      list.add(coreGen);

//      if (getHasRepository()) {
//        RepositoryGenerator repoGen = getRepositoryGenerator();
//        list.add(repoGen);
//      }
    }
    java.util.ArrayList<BaseGenerator> extendGenerators = createCefExtendGenerators();
    if (extendGenerators != null) {
      list.addAll(extendGenerators);
    }
    return list;
  }

  @Override
  protected void afterGenerate() {
    super.afterGenerate();
    if (!preGenerating) {
      CefConfigGenerator configGen = createConfigGenerator();
      if (configGen != null) {
        configGen.generate();
      }
    }
  }

  protected abstract CefConfigGenerator createConfigGenerator();

  protected java.util.ArrayList<BaseGenerator> createCefExtendGenerators() {
    return null;
  }

  protected abstract CefCoreGenerator createCoreGenerator();

  protected abstract BaseEntityGenerator createEntityGenerator();

  protected abstract CefApiGenerator createApiGenerator();

  @Override
  protected void beforeGenerate() {
    super.beforeGenerate();
    getContext().setRefProjectGenerator(refGenerator);
  }
}
